package com.mobile.collect.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.mobile.collect.R;
import com.mobile.collect.beans.DevicePointInfo;
import com.mobile.collect.db.EntityManager;
import com.mobile.collect.dialogs.SubmitTipDialog;
import com.mobile.collect.present.BasePresent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.base.BaseListActivity;
import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;

public class PointListActivity extends BaseListActivity<DevicePointInfo, BasePresent> {


    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.submitTv)
    Button submitTv;
    private Long deviceDbId;
    private Long parentId;
    private Long targetPicDbId;
    private boolean isSingel;
    private List<DevicePointInfo> selectList;

    @Override
    public void initView() {
        super.initView();
        titleTv.setText("选择复制点");
        if (isSingel) {
            rightIv.setVisibility(View.INVISIBLE);
        }
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setEnableLoadmore(false);
    }


    @Override
    public BaseRecyclerAdapter<DevicePointInfo> getAdapter() {
        return new BaseRecyclerAdapter<DevicePointInfo>(this) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_point;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                super.bindData(holder, position);
                DevicePointInfo info = getItemByPosition(position);
                holder.setText(R.id.titleTv, info.getDeviceName());
                holder.setText(R.id.descTv, info.getAdditionalDes());
                holder.setText(R.id.expandNoTv, "扩展号：" + info.getExpendNo() + " " + info.getChildTypeEn());
                if (selectList.contains(info)) {
                    holder.setImageResDrawable(R.id.selectIv, R.mipmap.select1);
                } else {
                    holder.setImageResDrawable(R.id.selectIv, R.mipmap.select0);
                }
            }
        };
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        selectList = new ArrayList<>();
        List<DevicePointInfo> list = EntityManager.getInstance().queryDevicePointInfoIByParentid(parentId);
        onSuccess(list);
    }

    @Override
    public void itemClick(int position) {
        DevicePointInfo info = mBaseAdapter.getItemByPosition(position);
        if (isSingel) {
            selectList.clear();
            selectList.add(info);
        } else {
            if (selectList.contains(info)) {
                selectList.remove(info);
            } else {
                selectList.add(info);
            }
        }
        checkSlelectCount();
    }

    private void checkSlelectCount() {
        if (selectList.size() == mBaseAdapter.getItemCount() && mBaseAdapter.getItemCount() != 0) {
            rightIv.setImageResource(R.mipmap.allselect1);
        } else {
            rightIv.setImageResource(R.mipmap.allselect0);
        }
        if (selectList.size() != 0) {
            submitTv.setVisibility(View.VISIBLE);
        } else {
            submitTv.setVisibility(View.GONE);
        }
        mBaseAdapter.notifyDataSetChanged();
    }


    @Override
    public int getContentViewId() {
        return R.layout.activity_pointlist;
    }

    @Override
    public void getDataFromServer() {

    }

    @Override
    public void getIntentData() {
        deviceDbId = getIntent().getLongExtra("deviceDbId", -1);
        parentId = getIntent().getLongExtra("parentid", -1);
        targetPicDbId = getIntent().getLongExtra("targetpicdbid", -1);
        isSingel = getIntent().getBooleanExtra("issingel", false);
    }

    @Override
    public BasePresent newP() {
        return new BasePresent();
    }

    public static void lunch(Activity context, Long deviceDbId, Long parentId, Long targetPicDbId, boolean isSingel) {
        Intent intent = new Intent(context, PointListActivity.class);
        intent.putExtra("deviceDbId", deviceDbId);
        intent.putExtra("parentid", parentId);
        intent.putExtra("targetpicdbid", targetPicDbId);
        intent.putExtra("issingel", isSingel);
        context.startActivityForResult(intent, 0x001);
    }

    @Override
    public int getTitleLayoutResId() {
        return 0;
    }

    @OnClick({R.id.rightIv, R.id.submitTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rightIv:
                if (selectList.size() == mBaseAdapter.getItemCount() && selectList.size() != 0) {
                    selectList.clear();
                } else {
                    selectList.addAll(mBaseAdapter.getmList());
                }
                checkSlelectCount();
                break;
            case R.id.submitTv:
                if (isSingel) {
                    Intent intent = new Intent();
                    intent.putExtra("copypointdbid", selectList.get(0).get_ID());
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                } else {
                    showTip();
                }
                break;
        }
    }

    public void copyPoints(String address) {
        for (DevicePointInfo info : selectList) {
            info.set_ID(null);
            info.setParentId(targetPicDbId);
            if (!TextUtils.isEmpty(address)) {
                info.setAdditionalDes(address);
            }
            info.setIsNewPoint(true);
        }
        savePoints();
    }

    private void savePoints(){
        EntityManager.getInstance().savePointInfo(selectList);
        getP().sendPointChagneEvent(deviceDbId,targetPicDbId);
        startActivity(new Intent(this, DevicePointsActivity.class));
        finish();
    }

    private void showTip() {
        SubmitTipDialog tipDialog = new SubmitTipDialog(this, new SubmitTipDialog.SlectClickListener() {
            @Override
            public void submitClick() {
                AddDescActivity.lunch(PointListActivity.this, 0x001);
            }

            @Override
            public void cancelClick() {
                copyPoints("");
//                savePoints();
            }
        }, "");
        tipDialog.display("提示", "是否修改附加描述?", "不修改", "修改");
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String adddesc = "";
        if (null != data) {
            adddesc = data.getStringExtra("adddesc");
        }
        showCopyTip(adddesc);
    }

    private void showCopyTip(final String adddesc) {
        String message = "附加描述已修改为[" + adddesc + "],是否复制?";

        if (TextUtils.isEmpty(adddesc)) {
            message = "您没有修改附加描述，是否提交！";
        }
        SubmitTipDialog tipDialog = new SubmitTipDialog(this, new SubmitTipDialog.SlectClickListener() {
            @Override
            public void submitClick() {
                copyPoints(adddesc);
            }

            @Override
            public void cancelClick() {

            }
        }, "");
        tipDialog.display("提示", message, "取消", "确定");
    }
}
